package com.cy.controller;


import com.cy.entity.Address;
import com.cy.service.IAddressService;
import com.cy.util.JsonResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.html.HTMLParagraphElement;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 时间：2023/1/6
 */
@RequestMapping("/addresses")
@RestController
public class AddressController extends BaseController {
    @Resource
    private IAddressService addressService;

    @RequestMapping("/add_new_address")
    public JsonResult<Void> addNewAddress(Address address, HttpSession httpSession){
        Integer uid = getuidFromSession(httpSession);
        String username = getUsernameFromSession(httpSession);
        addressService.addNewAddress(uid,username,address);
        return new JsonResult<>(OK);
    }

    @RequestMapping({"/",""})
    public JsonResult<List<Address>> getByUid(HttpSession session){
        Integer uid = getuidFromSession(session);
        List<Address> data = addressService.getByUid(uid);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/{aid}/set_default")
    public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid,HttpSession httpSession){
        addressService.setDefault(aid,getuidFromSession(httpSession),getUsernameFromSession(httpSession));
        return new JsonResult<>(OK);
    }

    @RequestMapping("/{aid}/delete")
    public JsonResult<Void> delete(@PathVariable("aid")Integer aid,HttpSession httpSession){
        addressService.delete(aid,getuidFromSession(httpSession),getUsernameFromSession(httpSession));
        return new JsonResult<>(OK);
    }


}
